﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using DataAnnotationsExtensions;

namespace MobiShop.Models
{
    public class ProductModels
    {
        // Hoàng
        // lấy về Product theo id, trả về null nếu không tìm thấy
        public static Product GetProductById(int id)
        {
            MobishopEntities content = new MobishopEntities();
            var query = from product in content.ProductList
                        where product.id == id && product.visible==true
                        select product;

            Product kq = null;
            if (query.Count() > 0) kq = query.First<Product>();
            return kq;
        }

        public static Product GetAllProductById(int id)
        {
            MobishopEntities content = new MobishopEntities();
            var query = content.ProductList.Where(product => product.id == id);
            Product kq = null;
            if (query.Count() > 0) kq = query.First<Product>();
            return kq;
        }

        // Hoàng
        // lấy về danh sách sản phẩm với ID được chỉ định trong List
        public static List<Product> GetProductsFromListID(List<int> listID)
        {
            MobishopEntities content = new MobishopEntities();
            return content.ProductList.Where(product => listID.Contains(product.id) && product.visible == true).ToList();
        }


        //  Đạt
        //  Lấy về 12 sản phẩm mới nhất
        public static List<Product> NewestList(int count)
        {
            MobishopEntities context = new MobishopEntities();
            var query = from product in context.ProductList
                        where product.visible == true // Hoàng: em thêm vào cái visible để sau này Hùng làm chức năng xóa
                        orderby product.createDate descending
                        select product;

            return query.Take(count).ToList<Product>();
        }
        
        //  Đạt
        //  Lấy ra 12 sản phẩm bán chạy nhất
        public static List<Product> BestSaleList(int count)
        {
            MobishopEntities context = new MobishopEntities();
            var query = from product in context.ProductList
                        where product.visible == true
                        orderby product.totalSale descending
                        select product;

            return query.Take(count).ToList<Product>();
        }

        // Đạt
        // Detail of product
        public static Product Details(int id)
        {
            MobishopEntities context = new MobishopEntities();
            var query = from product in context.ProductList
                        where product.id == id && product.visible==true
                        select product;
            if (query.Count() == 1) return query.First();
            else return null;
        }

        // lấy về danh sách các sản phẩm cùng loại với 1 sản phẩm cho trước
        // ghi chú : có thể làm tốt hơn bằng cách trả về 1 danh sách sản phẩm được chọn ngẫu niên
        public static List<Product> SanPhamCungNhaSanXuat(Product sp, int count)
        {
            MobishopEntities context = new MobishopEntities();
            var query = from product in context.ProductList
                        where product.id != sp.id && product.visible == true 
                        && product.manufactureId == sp.manufactureId
                        orderby product.id
                        select product;
            return query.Take(count).ToList<Product>();
        }


        //Đạt
        //List product by category
        public static List<Product> ProByCat(int id, int page, out int count)
        {
            MobishopEntities context = new MobishopEntities();
            var query = from listbycat in context.ProductList
                        where listbycat.categoryId == id && listbycat.visible == true
                        orderby listbycat.id ascending
                        select listbycat;
            int productperpage = 8;
            count = query.Count();
            return query.Skip((page - 1) * productperpage).Take(productperpage).ToList<Product>();
        }

        //Đạt
        //List product by Manufacturer
        public static List<Product> ProByManu(int id, int page, out int count)
        {
            MobishopEntities context = new MobishopEntities();
            var query = from listbymanu in context.ProductList
                        where listbymanu.manufactureId == id && listbymanu.visible == true
                        orderby listbymanu.id ascending
                        select listbymanu;
            int productperpage = 8;
            count = query.Count();
            return query.Skip((page - 1) * productperpage).Take(productperpage).ToList<Product>();
        }

        public static int CatTotalPage(int id)
        {
            MobishopEntities context = new MobishopEntities();
            var query = from listbycat in context.ProductList
                        where listbycat.categoryId == id && listbycat.visible == true
                        orderby listbycat.id ascending
                        select listbycat;

            return query.Count();
        }
        public static int ManuTotalPage(int id)
        {
            MobishopEntities context = new MobishopEntities();
            var query = from listbymanu in context.ProductList
                        where listbymanu.manufactureId == id && listbymanu.visible == true
                        orderby listbymanu.id ascending
                        select listbymanu;

            return query.Count();
        }  
     
        //Đạt
        //Search function

        public static List<Product> getProductByName(string phonename, int productperpage, int page, out int count)
        {
            MobishopEntities context = new MobishopEntities();
            var query = context.ProductList.Where(sp => sp.visible==true);
            if(phonename!= null && phonename.Length>0)
            {
                query = query.Where(sp => sp.name.Contains(phonename));
            }
            
            count = query.Count();
            return query.OrderBy(sp => sp.id).Skip((page-1)*productperpage).Take(productperpage).ToList<Product>();
        }

        public static List<Product> advanceSearch
            (string txtName, int?ddlManu, int? ddlCat, int? ddlRegion, decimal? txtPriceFrom, 
            decimal? txtPriceTo, int productperpage, int page, out int count)
        {
            MobishopEntities context = new MobishopEntities();
            var query = context.ProductList.Where(sp => sp.visible == true);

            if (txtName != null && txtName.Length > 0)
                query = query.Where(sp => sp.name.Contains(txtName));
            if (ddlManu != null)
                query = query.Where(sp => sp.manufactureId == ddlManu);
            if (ddlCat != null)
                query = query.Where(sp => sp.categoryId == ddlCat);
            if (ddlRegion != null)
                query = query.Where(sp => sp.regionId == ddlRegion);
            if (txtPriceFrom != null)
                query = query.Where(sp => sp.price >= txtPriceFrom);
            if (txtPriceTo != null)
                query = query.Where(sp => sp.price <= txtPriceTo);

            count = query.Count();
            return query.OrderBy(sp => sp.id).Skip((page - 1) * productperpage).Take(productperpage).ToList<Product>();
        }
    }

    public class FindProductData
    {
        [Display(Name = "Tên:")]
        public string name { get; set; }

        [Display(Name = "Nhà sản xuất:")]
        public int? manufactureId { get; set; }

        [Display(Name = "Loại:")]
        public int? categoryId { get; set; }

        [Display(Name = "Xuất xứ:")]
        public int? regionId { get; set; }

        [Display(Name = "Giá từ:")]
        [RegularExpression(@"^\d+$", ErrorMessage = "Số tiền từ phải là số nguyên >= 0 hợp lệ")]
        public decimal? priceFrom { get; set; }

        [Display(Name = "đến:")]
        [RegularExpression(@"^\d+$", ErrorMessage = " | Số tiền từ phải là số nguyên >= 0 hợp lệ")]
        public decimal? priceTo { get; set; }

        [Display(Name = "Tình trạng:")]
        public bool? visible { get; set; }

        public FindProductData()
        {
        }

        public IQueryable<Product> Find(MobishopEntities db)
        {
            var list = from s in db.ProductList
                       select s;
            // tiến hành lọc theo các tiêu chí tìm kiếm:
            if (name != null && name.Length>0)
                list = list.Where(s => s.name.Contains(name));
            if (manufactureId != null)
                list = list.Where(s => s.manufactureId == manufactureId);

            if (categoryId != null)
                list = list.Where(s => s.categoryId == categoryId);
            if (regionId != null)
                list = list.Where(s => s.regionId == regionId);

            if (priceFrom != null)
                list = list.Where(s => s.price >= priceFrom);
            if (priceTo != null)
                list = list.Where(s => s.price <= priceTo);
            if (visible != null)
            {
                if ((bool)visible)
                    list = list.Where(s => s.visible == true);
                else
                    list = list.Where(s => s.visible == false || s.visible == null);
            }

            return list;
        }
    }
}